/**************************************************************

Copyright Astrium Services GEO-Information / Spot Image
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

 ***************************************************************/
package com.spotimage.catalog.client.ui.listgrid;

import com.smartgwt.client.data.Record;
import com.smartgwt.client.widgets.grid.events.SelectionChangedHandler;
import com.smartgwt.client.widgets.grid.events.SelectionEvent;
import com.smartgwt.client.widgets.menu.MenuItem;
import com.smartgwt.client.widgets.menu.events.ClickHandler;
import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent;
import com.spotimage.catalog.client.event.DeselectedSelectionEvent;
import com.spotimage.catalog.client.event.EventBus;
import com.spotimage.catalog.client.event.RemoveFromSelectionEvent;
import com.spotimage.catalog.client.event.SelectedSelectionEvent;
import com.spotimage.catalog.client.util.Util;

/**
 * Selection list of scenes
 */
public class SelectionListGrid extends SceneListGrid {

	/**
	 * Set context menu.
	 */
	public SelectionListGrid() {
		super(true);
		
		// context menu
		MenuItem remove = new MenuItem("Remove from selection");
		remove.setIcon(Util.getImageUrl("cart-remove.png"));
		remove.addClickHandler(new ClickHandler() {
			//@Override
			public void onClick(MenuItemClickEvent event) {
				SceneListGridRecord selectedRecord = (SceneListGridRecord) getSelectedRecord();
				removeData(selectedRecord);
				EventBus.getInstance().fireEvent(new RemoveFromSelectionEvent(selectedRecord));
			}
		});
		contextMenu.addItem(remove, 0);
		MenuItem removeAll = new MenuItem("Remove all");
		removeAll.addClickHandler(new com.smartgwt.client.widgets.menu.events.ClickHandler() {
			//@Override
			public void onClick(MenuItemClickEvent event) {
				while (getRecordList().getLength() > 0) {
					Record record = getRecordList().get(0);
					removeData(record);
					EventBus.getInstance().fireEvent(new RemoveFromSelectionEvent((SceneListGridRecord) record));
				}
			}
		});
		contextMenu.addItem(removeAll);
		
		// handlers
		addSelectionChangedHandler(new SelectionChangedHandler() {
			//@Override
			public void onSelectionChanged(SelectionEvent event) {
				SceneListGridRecord sceneListGridRecord = (SceneListGridRecord) event.getRecord();
				if (!event.getState()) {
					// item is deselected
					EventBus.getInstance().fireEvent(new DeselectedSelectionEvent(sceneListGridRecord.getId()));
				} else {
					// item is selected
					EventBus.getInstance().fireEvent(new SelectedSelectionEvent(sceneListGridRecord.getId()));
				}
			}
		});
	}
	
}
